package com.sdstc.modules.uitl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.ruoyi.common.enums.MechanismLevel;
import com.ruoyi.common.utils.ConvertUtil;
import com.sdstc.modules.eneity.OrgEntity;
import com.sdstc.modules.vo.OrgTreeSelectVo;
import com.sdstc.modules.vo.OrgTreeVo;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author ：wk
 * @date ：Created in 2022/10/29 17:04
 */
public class TreeUtils {

    public static Map<String,List<OrgTreeVo>> orgId2ListOrgTreeVo = new HashMap<>(20000);

    public static TreeUtils getInstance(){
        return new TreeUtils();
    }

    public static Map<String,List<OrgTreeVo>> getOrgId2ListOrgTreeVo(){
        return orgId2ListOrgTreeVo;
    }

    // 获取所有的数据的结点，将子节点放在children中

    public  List<OrgTreeVo> getOrgTree(String orgId,List<OrgEntity> list) {

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<OrgTreeVo> resultTree = new ArrayList<>();

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                orgTreeVo.setChildren(loadChild(orgTreeVo, orgTreeVos));
                resultTree.add(orgTreeVo);
            }
        }

        return resultTree;
    }

    public List<OrgTreeVo> loadChild(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        List<OrgTreeVo> children = new ArrayList<>();

        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                treeVo.setChildren(loadChild(treeVo, orgTreeVos));
                children.add(treeVo);
            }
        }

        return children;
    }



    // 获取所有的数据的结点，将子节点不放在children中
    public  List<String> getAllOrgIds(String orgId,List<OrgEntity> list) {

        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>(0);
        }

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<String> resultOrgIds = new ArrayList<>(list.size());

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
               resultOrgIds.addAll(loadChildOrgIds(orgTreeVo, orgTreeVos));
                resultOrgIds.add(orgTreeVo.getId());
            }
        }

        return resultOrgIds;
    }

    public List<String> loadChildOrgIds(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        List<String> children = new ArrayList<>();

        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                children.addAll(loadChildOrgIds(treeVo, orgTreeVos));
                children.add(treeVo.getId());
            }
        }

        return children;
    }

    // 获取所有的数据的结点，将子节点不放在children中
    public static List<OrgTreeVo> getAllOrg(String orgId, List<OrgEntity> list) {

        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>(0);
        }

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<OrgTreeVo> resultOrgIds = new ArrayList<>(list.size());

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                resultOrgIds.addAll(loadChildOrg(orgTreeVo, orgTreeVos));
                resultOrgIds.add(orgTreeVo);
            }
        }

        return resultOrgIds;
    }

    public static List<OrgTreeVo> loadChildOrg(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        List<OrgTreeVo> children = new ArrayList<>();

        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                children.addAll(loadChildOrg(treeVo, orgTreeVos));
                children.add(treeVo);
            }
        }

        return children;
    }

    public static List<OrgTreeVo> getAllOrgScreen(String orgId, List<OrgEntity> list) {

        if (CollectionUtils.isEmpty(list)){
            return new ArrayList<>(0);
        }

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<OrgTreeVo> resultOrgIds = new ArrayList<>(list.size());

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                List<OrgTreeVo> orgTreeVos1 = loadChildOrg(orgTreeVo, orgTreeVos);
                resultOrgIds.addAll(orgTreeVos1);
                orgId2ListOrgTreeVo.put(orgTreeVo.getId(),orgTreeVos1);
                resultOrgIds.add(orgTreeVo);
            }
        }

        return resultOrgIds;
    }




    // 获取下属机构的实配数量
    public  Integer getAllOrgOrganCount(String orgId,List<OrgEntity> list) {

        if (CollectionUtils.isEmpty(list)){
            return 0;
        }

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        Integer count  = 0;

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                count +=loadChildOrgOrgCount(orgTreeVo, orgTreeVos);
                count+= OrgTreeVo.orgCount(orgTreeVo);
            }
        }

        return count;
    }

    public Integer loadChildOrgOrgCount(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        Integer integer  = 0;


        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                integer +=loadChildOrgOrgCount(orgTreeVo, orgTreeVos);
                integer+= OrgTreeVo.orgCount(orgTreeVo);
            }
        }

        return integer;
    }


    // 获取处级以上的机构列表
    public  List<OrgTreeSelectVo> getOrgTreeSelect(String orgId, List<OrgEntity> list) {

        List<OrgTreeSelectVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeSelectVo.class);

        List<String> upLevel = new ArrayList<>();
        upLevel.add(MechanismLevel.BUREAUDEPUTY.getCode());
        upLevel.add(MechanismLevel.DIVISION.getCode());
        upLevel.add(MechanismLevel.DIVISIONDEPUTY.getCode());

        orgTreeVos = orgTreeVos.stream().filter(p -> upLevel.contains(p.getOrgLevel())).collect(Collectors.toList());

        // 结果树
        List<OrgTreeSelectVo> resultTree = new ArrayList<>();

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeSelectVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid) && upLevel.contains(orgTreeVo.getOrgLevel())) {
                orgTreeVo.setLabel(orgTreeVo.getOrgName());
                orgTreeVo.setChildren(loadTreeSelectChild(orgTreeVo,orgTreeVos));
                resultTree.add(orgTreeVo);
            }
        }

        return resultTree;
    }

    public List<OrgTreeSelectVo> loadTreeSelectChild(OrgTreeSelectVo orgTreeVo, List<OrgTreeSelectVo> orgTreeVos) {
        List<OrgTreeSelectVo> children = new ArrayList<>();

        for (OrgTreeSelectVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                treeVo.setLabel(treeVo.getOrgName());
                treeVo.setChildren(loadTreeSelectChild(treeVo, orgTreeVos));
                children.add(treeVo);
            }
        }

        return children;
    }
    
    /**
    *@author: wk
    *@date: 2023/11/8 15:02
    *@description: 如果子节点名字包含父节点的名字，子节点去掉父节点的名字，
     * 例如济南市公安局历下区分局的父节点为济南市公安局，那么就改为历下区分局
    */
    public  List<OrgTreeVo> getOrgSimpleTree(String orgId,List<OrgEntity> list) {

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<OrgTreeVo> resultTree = new ArrayList<>();

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                orgTreeVo.setChildren(loadSimpleChild(orgTreeVo, orgTreeVos));
                for (OrgTreeVo child : orgTreeVo.getChildren()) {
                    if (child.getOrgName().contains(orgTreeVo.getOrgName())) {
                        child.setSimpleName(child.getOrgName().substring(orgTreeVo.getOrgName().length()));
                    }
                }
                resultTree.add(orgTreeVo);
            }
        }

        return resultTree;
    }

    public List<OrgTreeVo> loadSimpleChild(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        List<OrgTreeVo> children = new ArrayList<>();

        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                treeVo.setChildren(loadSimpleChild(treeVo, orgTreeVos));
                for (OrgTreeVo child : treeVo.getChildren()) {
                    if (child.getOrgName().contains(treeVo.getOrgName())) {
                        child.setSimpleName(child.getOrgName().substring(treeVo.getOrgName().length()));
                    }
                }
                children.add(treeVo);
            }
        }

        return children;
    }

    public  List<OrgTreeVo> getOrgChartTree(String orgId,List<OrgEntity> list) {

        list = list.stream().filter(l -> !Objects.equals(l.getOrgName(), "部门领导") && !Objects.equals(l.getOrgName(), "分局领导")).collect(Collectors.toList());

        List<OrgTreeVo> orgTreeVos = ConvertUtil.entityToVoList(list, OrgTreeVo.class);
        // 结果树
        List<OrgTreeVo> resultTree = new ArrayList<>();

        String pid = Optional.ofNullable(orgId).orElse("0");

        for (OrgTreeVo orgTreeVo : orgTreeVos) {
            if (Objects.equals(orgTreeVo.getPid(), pid)) {
                orgTreeVo.setChildren(loadChartChild(orgTreeVo, orgTreeVos));
                for (OrgTreeVo child : orgTreeVo.getChildren()) {
                    if (child.getOrgName().contains(orgTreeVo.getOrgName())) {
                        child.setSimpleName(child.getOrgName().substring(orgTreeVo.getOrgName().length()));
                    }
                }
                resultTree.add(orgTreeVo);
            }
        }

        return resultTree;
    }

    public List<OrgTreeVo> loadChartChild(OrgTreeVo orgTreeVo, List<OrgTreeVo> orgTreeVos) {
        List<OrgTreeVo> children = new ArrayList<>();

        for (OrgTreeVo treeVo : orgTreeVos) {
            if (Objects.equals(treeVo.getPid(), orgTreeVo.getId())) {
                treeVo.setChildren(loadSimpleChild(treeVo, orgTreeVos));
                for (OrgTreeVo child : treeVo.getChildren()) {
                    if (child.getOrgName().contains(treeVo.getOrgName())) {
                        child.setSimpleName(child.getOrgName().substring(treeVo.getOrgName().length()));
                    }
                }
                children.add(treeVo);
            }
        }

        return children;
    }
}
